package org.stragma.symbian.apply.stratego;

import org.strategoxt.stratego_lib.*;
import org.strategoxt.stratego_lib.*;
import org.strategoxt.lang.*;
import org.spoofax.interpreter.terms.*;
import static org.strategoxt.lang.Term.*;
import org.spoofax.interpreter.library.AbstractPrimitive;
import java.util.ArrayList;
import java.lang.ref.WeakReference;

@SuppressWarnings("all") public class DesugarJ  
{ 
  protected static final boolean TRACES_ENABLED = true;

  protected static ITermFactory constantFactory;

  private static WeakReference<Context> initedContext;

  private static boolean isIniting;

  protected static IStrategoTerm const2;

  protected static IStrategoTerm const1;

  protected static IStrategoTerm const0;

  public static IStrategoConstructor _consConc_2;

  public static IStrategoConstructor _consNone_0;

  public static IStrategoConstructor _consSome_1;

  public static IStrategoConstructor _consRange_2;

  public static IStrategoConstructor _consRange_1;

  public static IStrategoConstructor _consIn_2;

  public static IStrategoConstructor _consOut_2;

  public static IStrategoConstructor _consInOut_2;

  public static IStrategoConstructor _consDeclaration_2;

  public static IStrategoConstructor _consVarList_1;

  public static IStrategoConstructor _consAccess_2;

  public static IStrategoConstructor _consArgument_2;

  public static Context init(Context context)
  { 
    synchronized(DesugarJ.class)
    { 
      if(isIniting)
        return null;
      try
      { 
        isIniting = true;
        ITermFactory termFactory = context.getFactory();
        if(constantFactory == null)
        { 
          initConstructors(termFactory);
          initConstants(termFactory);
        }
        if(initedContext == null || initedContext.get() != context)
        { 
          org.strategoxt.stratego_lib.Main.init(context);
          org.strategoxt.stratego_lib.Main.init(context);
          context.registerComponent("DesugarJ");
        }
        initedContext = new WeakReference<Context>(context);
        constantFactory = termFactory;
      }
      finally
      { 
        isIniting = false;
      }
      return context;
    }
  }

  public static Context init()
  { 
    return init(new Context());
  }

  public static void main(String args[])
  { 
    Context context = init();
    context.setStandAlone(false);
    try
    { 
      IStrategoTerm result;
      try
      { 
        result = context.invokeStrategyCLI(io_$Desugar_0_0.instance, "DesugarJ", args);
      }
      finally
      { 
        context.getIOAgent().closeAllFiles();
      }
      if(result == null)
      { 
        System.err.println("DesugarJ" + (TRACES_ENABLED ? ": rewriting failed, trace:" : ": rewriting failed"));
        context.printStackTrace();
        context.setStandAlone(false);
        System.exit(1);
      }
      else
      { 
        //System.out.println(result);
        context.setStandAlone(false);
        System.exit(0);
      }
    }
    catch(StrategoExit exit)
    { 
      context.setStandAlone(false);
      //System.exit(exit.getValue());
    }
  }

  public static IStrategoTerm mainNoExit(String ... args) throws StrategoExit
  { 
    return mainNoExit(new Context(), args);
  }

  public static IStrategoTerm mainNoExit(Context context, String ... args) throws StrategoExit
  { 
    try
    { 
      init(context);
      return context.invokeStrategyCLI(io_$Desugar_0_0.instance, "DesugarJ", args);
    }
    finally
    { 
      context.getIOAgent().closeAllFiles();
    }
  }

  public static Strategy getMainStrategy()
  { 
    return io_$Desugar_0_0.instance;
  }

  public static void initConstructors(ITermFactory termFactory)
  { 
    _consConc_2 = termFactory.makeConstructor("Conc", 2);
    _consNone_0 = termFactory.makeConstructor("None", 0);
    _consSome_1 = termFactory.makeConstructor("Some", 1);
    _consRange_2 = termFactory.makeConstructor("Range", 2);
    _consRange_1 = termFactory.makeConstructor("Range", 1);
    _consIn_2 = termFactory.makeConstructor("In", 2);
    _consOut_2 = termFactory.makeConstructor("Out", 2);
    _consInOut_2 = termFactory.makeConstructor("InOut", 2);
    _consDeclaration_2 = termFactory.makeConstructor("Declaration", 2);
    _consVarList_1 = termFactory.makeConstructor("VarList", 1);
    _consAccess_2 = termFactory.makeConstructor("Access", 2);
    _consArgument_2 = termFactory.makeConstructor("Argument", 2);
  }

  public static void initConstants(ITermFactory termFactory)
  { 
    const0 = termFactory.makeString("In");
    const1 = termFactory.makeString("Out");
    const2 = termFactory.makeString("InOut");
  }

  @SuppressWarnings("all") public static class io_$Desugar_0_0 extends Strategy 
  { 
    public static io_$Desugar_0_0 instance = new io_$Desugar_0_0();

    @Override public IStrategoTerm invoke(Context context, IStrategoTerm term)
    { 
      context.push("io_Desugar_0_0");
      Fail0:
      { 
        term = io_wrap_1_0.instance.invoke(context, term, $Apply__$Desugar_0_0.instance);
        if(term == null)
          break Fail0;
        context.popOnSuccess();
        if(true)
          return term;
      }
      context.popOnFailure();
      return null;
    }
  }

  @SuppressWarnings("all") public static class x_99 extends Strategy 
  { 
    public static x_99 instance = new x_99();

    @Override public IStrategoTerm invoke(Context context, IStrategoTerm term)
    { 
      ITermFactory termFactory = context.getFactory();
      Fail1:
      { 
        IStrategoTerm term0 = term;
        Success0:
        { 
          Fail2:
          { 
            IStrategoTerm v_99 = null;
            if(term.getTermType() != IStrategoTerm.APPL || DesugarJ._consRange_1 != ((IStrategoAppl)term).getConstructor())
              break Fail2;
            v_99 = term.getSubterm(0);
            term = termFactory.makeAppl(DesugarJ._consRange_2, new IStrategoTerm[]{v_99, v_99});
            if(true)
              break Success0;
          }
          term = term0;
        }
        if(true)
          return term;
      }
      context.push("x_99");
      context.popOnFailure();
      return null;
    }
  }

  @SuppressWarnings("all") public static class $Apply__$Desugar_0_0 extends Strategy 
  { 
    public static $Apply__$Desugar_0_0 instance = new $Apply__$Desugar_0_0();

    @Override public IStrategoTerm invoke(Context context, IStrategoTerm term)
    { 
      context.push("Apply__Desugar_0_0");
      Fail3:
      { 
        term = topdown_1_0.instance.invoke(context, term, lifted1.instance);
        if(term == null)
          break Fail3;
        term = try_1_0.instance.invoke(context, term, $Desugar$Arg$Var_0_0.instance);
        if(term == null)
          break Fail3;
        term = w_99.instance.invoke(context, term, x_99.instance);
        if(term == null)
          break Fail3;
        context.popOnSuccess();
        if(true)
          return term;
      }
      context.popOnFailure();
      return null;
    }
  }

  @SuppressWarnings("all") public static class $Desugar$Arg$Var_0_0 extends Strategy 
  { 
    public static $Desugar$Arg$Var_0_0 instance = new $Desugar$Arg$Var_0_0();

    @Override public IStrategoTerm invoke(Context context, IStrategoTerm term)
    { 
      context.push("DesugarArgVar_0_0");
      Fail4:
      { 
        term = try_1_0.instance.invoke(context, term, $Desugar$Var_0_0.instance);
        if(term == null)
          break Fail4;
        term = try_1_0.instance.invoke(context, term, $Desugar$Arg_0_0.instance);
        if(term == null)
          break Fail4;
        term = SRTS_all.instance.invoke(context, term, $Desugar$Arg$Var_0_0.instance);
        if(term == null)
          break Fail4;
        context.popOnSuccess();
        if(true)
          return term;
      }
      context.popOnFailure();
      return null;
    }
  }

  @SuppressWarnings("all") public static class $Desugar$Range_0_0 extends Strategy 
  { 
    public static $Desugar$Range_0_0 instance = new $Desugar$Range_0_0();

    @Override public IStrategoTerm invoke(Context context, IStrategoTerm term)
    { 
      ITermFactory termFactory = context.getFactory();
      Fail5:
      { 
        IStrategoTerm z_5 = null;
        if(term.getTermType() != IStrategoTerm.APPL || DesugarJ._consRange_1 != ((IStrategoAppl)term).getConstructor())
          break Fail5;
        z_5 = term.getSubterm(0);
        term = termFactory.makeAppl(DesugarJ._consRange_2, new IStrategoTerm[]{z_5, z_5});
        if(true)
          return term;
      }
      context.push("DesugarRange_0_0");
      context.popOnFailure();
      return null;
    }
  }

  @SuppressWarnings("all") public static class $Desugar$Var_0_0 extends Strategy 
  { 
    public static $Desugar$Var_0_0 instance = new $Desugar$Var_0_0();

    @Override public IStrategoTerm invoke(Context context, IStrategoTerm term)
    { 
      ITermFactory termFactory = context.getFactory();
      context.push("DesugarVar_0_0");
      Fail6:
      { 
        IStrategoTerm term1 = term;
        Success1:
        { 
          Fail7:
          { 
            IStrategoTerm c_6 = null;
            IStrategoTerm d_6 = null;
            IStrategoTerm e_6 = null;
            IStrategoTerm f_6 = null;
            IStrategoTerm g_6 = null;
            IStrategoTerm h_6 = null;
            if(term.getTermType() != IStrategoTerm.LIST || ((IStrategoList)term).isEmpty())
              break Fail7;
            IStrategoTerm arg0 = ((IStrategoList)term).head();
            if(arg0.getTermType() != IStrategoTerm.APPL || DesugarJ._consDeclaration_2 != ((IStrategoAppl)arg0).getConstructor())
              break Fail7;
            IStrategoTerm arg1 = arg0.getSubterm(0);
            if(arg1.getTermType() != IStrategoTerm.APPL || DesugarJ._consVarList_1 != ((IStrategoAppl)arg1).getConstructor())
              break Fail7;
            IStrategoTerm arg2 = arg1.getSubterm(0);
            if(arg2.getTermType() != IStrategoTerm.LIST || ((IStrategoList)arg2).isEmpty())
              break Fail7;
            c_6 = ((IStrategoList)arg2).head();
            d_6 = ((IStrategoList)arg2).tail();
            e_6 = arg0.getSubterm(1);
            f_6 = ((IStrategoList)term).tail();
            h_6 = term;
            IStrategoList list0;
            list0 = checkListTail(f_6);
            if(list0 == null)
              break Fail7;
            term = (IStrategoTerm)termFactory.makeListCons(termFactory.makeAppl(DesugarJ._consDeclaration_2, new IStrategoTerm[]{termFactory.makeAppl(DesugarJ._consVarList_1, new IStrategoTerm[]{d_6}), e_6}), list0);
            term = this.invoke(context, term);
            if(term == null)
              break Fail7;
            g_6 = term;
            term = h_6;
            IStrategoList list1;
            list1 = checkListTail(g_6);
            if(list1 == null)
              break Fail7;
            term = (IStrategoTerm)termFactory.makeListCons(termFactory.makeAppl(DesugarJ._consDeclaration_2, new IStrategoTerm[]{c_6, e_6}), list1);
            if(true)
              break Success1;
          }
          term = term1;
          IStrategoTerm term2 = term;
          Success2:
          { 
            Fail8:
            { 
              IStrategoTerm b_6 = null;
              if(term.getTermType() != IStrategoTerm.LIST || ((IStrategoList)term).isEmpty())
                break Fail8;
              IStrategoTerm arg3 = ((IStrategoList)term).head();
              if(arg3.getTermType() != IStrategoTerm.APPL || DesugarJ._consDeclaration_2 != ((IStrategoAppl)arg3).getConstructor())
                break Fail8;
              IStrategoTerm arg4 = arg3.getSubterm(0);
              if(arg4.getTermType() != IStrategoTerm.APPL || DesugarJ._consVarList_1 != ((IStrategoAppl)arg4).getConstructor())
                break Fail8;
              IStrategoTerm arg5 = arg4.getSubterm(0);
              if(arg5.getTermType() != IStrategoTerm.LIST || !((IStrategoList)arg5).isEmpty())
                break Fail8;
              b_6 = ((IStrategoList)term).tail();
              term = this.invoke(context, b_6);
              if(term == null)
                break Fail8;
              if(true)
                break Success2;
            }
            term = term2;
            if(term.getTermType() != IStrategoTerm.LIST || !((IStrategoList)term).isEmpty())
              break Fail6;
          }
        }
        context.popOnSuccess();
        if(true)
          return term;
      }
      context.popOnFailure();
      return null;
    }
  }

  @SuppressWarnings("all") public static class $Desugar$Arg_0_0 extends Strategy 
  { 
    public static $Desugar$Arg_0_0 instance = new $Desugar$Arg_0_0();

    @Override public IStrategoTerm invoke(Context context, IStrategoTerm term)
    { 
      ITermFactory termFactory = context.getFactory();
      context.push("DesugarArg_0_0");
      Fail9:
      { 
        IStrategoTerm term3 = term;
        Success3:
        { 
          Fail10:
          { 
            IStrategoTerm k_6 = null;
            IStrategoTerm l_6 = null;
            IStrategoTerm m_6 = null;
            IStrategoTerm n_6 = null;
            IStrategoTerm o_6 = null;
            IStrategoTerm p_6 = null;
            if(term.getTermType() != IStrategoTerm.LIST || ((IStrategoList)term).isEmpty())
              break Fail10;
            IStrategoTerm arg6 = ((IStrategoList)term).head();
            if(arg6.getTermType() != IStrategoTerm.APPL || DesugarJ._consArgument_2 != ((IStrategoAppl)arg6).getConstructor())
              break Fail10;
            IStrategoTerm arg7 = arg6.getSubterm(0);
            if(arg7.getTermType() != IStrategoTerm.APPL || DesugarJ._consVarList_1 != ((IStrategoAppl)arg7).getConstructor())
              break Fail10;
            IStrategoTerm arg8 = arg7.getSubterm(0);
            if(arg8.getTermType() != IStrategoTerm.LIST || ((IStrategoList)arg8).isEmpty())
              break Fail10;
            k_6 = ((IStrategoList)arg8).head();
            l_6 = ((IStrategoList)arg8).tail();
            m_6 = arg6.getSubterm(1);
            n_6 = ((IStrategoList)term).tail();
            p_6 = term;
            IStrategoList list2;
            list2 = checkListTail(n_6);
            if(list2 == null)
              break Fail10;
            term = (IStrategoTerm)termFactory.makeListCons(termFactory.makeAppl(DesugarJ._consArgument_2, new IStrategoTerm[]{termFactory.makeAppl(DesugarJ._consVarList_1, new IStrategoTerm[]{l_6}), m_6}), list2);
            term = this.invoke(context, term);
            if(term == null)
              break Fail10;
            o_6 = term;
            term = p_6;
            IStrategoList list3;
            list3 = checkListTail(o_6);
            if(list3 == null)
              break Fail10;
            term = (IStrategoTerm)termFactory.makeListCons(termFactory.makeAppl(DesugarJ._consArgument_2, new IStrategoTerm[]{k_6, m_6}), list3);
            if(true)
              break Success3;
          }
          term = term3;
          IStrategoTerm term4 = term;
          Success4:
          { 
            Fail11:
            { 
              IStrategoTerm j_6 = null;
              if(term.getTermType() != IStrategoTerm.LIST || ((IStrategoList)term).isEmpty())
                break Fail11;
              IStrategoTerm arg9 = ((IStrategoList)term).head();
              if(arg9.getTermType() != IStrategoTerm.APPL || DesugarJ._consArgument_2 != ((IStrategoAppl)arg9).getConstructor())
                break Fail11;
              IStrategoTerm arg10 = arg9.getSubterm(0);
              if(arg10.getTermType() != IStrategoTerm.APPL || DesugarJ._consVarList_1 != ((IStrategoAppl)arg10).getConstructor())
                break Fail11;
              IStrategoTerm arg11 = arg10.getSubterm(0);
              if(arg11.getTermType() != IStrategoTerm.LIST || !((IStrategoList)arg11).isEmpty())
                break Fail11;
              j_6 = ((IStrategoList)term).tail();
              term = this.invoke(context, j_6);
              if(term == null)
                break Fail11;
              if(true)
                break Success4;
            }
            term = term4;
            if(term.getTermType() != IStrategoTerm.LIST || !((IStrategoList)term).isEmpty())
              break Fail9;
          }
        }
        context.popOnSuccess();
        if(true)
          return term;
      }
      context.popOnFailure();
      return null;
    }
  }

  @SuppressWarnings("all") public static class $Desugar$In$Out_0_0 extends Strategy 
  { 
    public static $Desugar$In$Out_0_0 instance = new $Desugar$In$Out_0_0();

    @Override public IStrategoTerm invoke(Context context, IStrategoTerm term)
    { 
      ITermFactory termFactory = context.getFactory();
      Fail12:
      { 
        IStrategoTerm term5 = term;
        IStrategoConstructor cons1 = term.getTermType() == IStrategoTerm.APPL ? ((IStrategoAppl)term).getConstructor() : null;
        Success5:
        { 
          if(cons1 == DesugarJ._consIn_2)
          { 
            Fail13:
            { 
              IStrategoTerm u_6 = null;
              IStrategoTerm v_6 = null;
              u_6 = term.getSubterm(0);
              v_6 = term.getSubterm(1);
              term = termFactory.makeAppl(DesugarJ._consArgument_2, new IStrategoTerm[]{u_6, termFactory.makeAppl(DesugarJ._consAccess_2, new IStrategoTerm[]{DesugarJ.const0, v_6})});
              if(true)
                break Success5;
            }
            term = term5;
          }
          Success6:
          { 
            if(cons1 == DesugarJ._consOut_2)
            { 
              Fail14:
              { 
                IStrategoTerm s_6 = null;
                IStrategoTerm t_6 = null;
                s_6 = term.getSubterm(0);
                t_6 = term.getSubterm(1);
                term = termFactory.makeAppl(DesugarJ._consArgument_2, new IStrategoTerm[]{s_6, termFactory.makeAppl(DesugarJ._consAccess_2, new IStrategoTerm[]{DesugarJ.const1, t_6})});
                if(true)
                  break Success6;
              }
              term = term5;
            }
            if(cons1 == DesugarJ._consInOut_2)
            { 
              IStrategoTerm q_6 = null;
              IStrategoTerm r_6 = null;
              q_6 = term.getSubterm(0);
              r_6 = term.getSubterm(1);
              term = termFactory.makeAppl(DesugarJ._consArgument_2, new IStrategoTerm[]{q_6, termFactory.makeAppl(DesugarJ._consAccess_2, new IStrategoTerm[]{DesugarJ.const2, r_6})});
            }
            else
            { 
              break Fail12;
            }
          }
        }
        if(true)
          return term;
      }
      context.push("DesugarInOut_0_0");
      context.popOnFailure();
      return null;
    }
  }

  @SuppressWarnings("all") public static class main_0_0 extends Strategy 
  { 
    public static main_0_0 instance = new main_0_0();

    @Override public IStrategoTerm invoke(Context context, IStrategoTerm term)
    { 
      context.push("main_0_0");
      Fail15:
      { 
        term = io_$Desugar_0_0.instance.invoke(context, term);
        if(term == null)
          break Fail15;
        context.popOnSuccess();
        if(true)
          return term;
      }
      context.popOnFailure();
      return null;
    }
  }

  @SuppressWarnings("all") private static final class lifted1 extends Strategy 
  { 
    public static final lifted1 instance = new lifted1();

    @Override public IStrategoTerm invoke(Context context, IStrategoTerm term)
    { 
      Fail16:
      { 
        term = try_1_0.instance.invoke(context, term, $Desugar$In$Out_0_0.instance);
        if(term == null)
          break Fail16;
        if(true)
          return term;
      }
      return null;
    }
  }

  @SuppressWarnings("all") private static final class w_99 extends Strategy 
  { 
    public static final w_99 instance = new w_99();

    @Override public IStrategoTerm invoke(Context context, IStrategoTerm term, Strategy y_99)
    { 
      Fail17:
      { 
        lifted4 lifted40 = new lifted4();
        lifted40.y_99 = y_99;
        term = SRTS_all.instance.invoke(context, term, lifted40);
        if(term == null)
          break Fail17;
        term = y_99.invoke(context, term);
        if(term == null)
          break Fail17;
        if(true)
          return term;
      }
      return null;
    }
  }

  @SuppressWarnings("all") private static final class lifted4 extends Strategy 
  { 
    Strategy y_99;

    @Override public IStrategoTerm invoke(Context context, IStrategoTerm term)
    { 
      Fail18:
      { 
        term = w_99.instance.invoke(context, term, y_99);
        if(term == null)
          break Fail18;
        if(true)
          return term;
      }
      return null;
    }
  }

  public static void registerInterop(org.spoofax.interpreter.core.IContext context, Context compiledContext)
  { 
    new InteropRegisterer().registerLazy(context, compiledContext, InteropRegisterer.class.getClassLoader());
  }

  @SuppressWarnings("unused") public static class InteropRegisterer extends org.strategoxt.lang.InteropRegisterer 
  { 
    @Override public void register(org.spoofax.interpreter.core.IContext context, Context compiledContext)
    { 
      register(context, compiledContext, context.getVarScope());
    }

    @Override public void registerLazy(org.spoofax.interpreter.core.IContext context, Context compiledContext, ClassLoader classLoader)
    { 
      registerLazy(context, compiledContext, classLoader, context.getVarScope());
    }

    private void register(org.spoofax.interpreter.core.IContext context, Context compiledContext, org.spoofax.interpreter.core.VarScope varScope)
    { 
      compiledContext.registerComponent("DesugarJ");
      DesugarJ.init(compiledContext);
      varScope.addSVar("io_Desugar_0_0", new InteropSDefT(io_$Desugar_0_0.instance, context));
      varScope.addSVar("x_99", new InteropSDefT(x_99.instance, context));
      varScope.addSVar("Apply__Desugar_0_0", new InteropSDefT($Apply__$Desugar_0_0.instance, context));
      varScope.addSVar("DesugarArgVar_0_0", new InteropSDefT($Desugar$Arg$Var_0_0.instance, context));
      varScope.addSVar("DesugarRange_0_0", new InteropSDefT($Desugar$Range_0_0.instance, context));
      varScope.addSVar("DesugarVar_0_0", new InteropSDefT($Desugar$Var_0_0.instance, context));
      varScope.addSVar("DesugarArg_0_0", new InteropSDefT($Desugar$Arg_0_0.instance, context));
      varScope.addSVar("DesugarInOut_0_0", new InteropSDefT($Desugar$In$Out_0_0.instance, context));
      varScope.addSVar("main_0_0", new InteropSDefT(main_0_0.instance, context));
    }

    private void registerLazy(org.spoofax.interpreter.core.IContext context, Context compiledContext, ClassLoader classLoader, org.spoofax.interpreter.core.VarScope varScope)
    { 
      compiledContext.registerComponent("DesugarJ");
      DesugarJ.init(compiledContext);
      varScope.addSVar("io_Desugar_0_0", new InteropSDefT(classLoader, "DesugarJ$io_$Desugar_0_0", context));
      varScope.addSVar("x_99", new InteropSDefT(classLoader, "DesugarJ$x_99", context));
      varScope.addSVar("Apply__Desugar_0_0", new InteropSDefT(classLoader, "DesugarJ$$Apply__$Desugar_0_0", context));
      varScope.addSVar("DesugarArgVar_0_0", new InteropSDefT(classLoader, "DesugarJ$$Desugar$Arg$Var_0_0", context));
      varScope.addSVar("DesugarRange_0_0", new InteropSDefT(classLoader, "DesugarJ$$Desugar$Range_0_0", context));
      varScope.addSVar("DesugarVar_0_0", new InteropSDefT(classLoader, "DesugarJ$$Desugar$Var_0_0", context));
      varScope.addSVar("DesugarArg_0_0", new InteropSDefT(classLoader, "DesugarJ$$Desugar$Arg_0_0", context));
      varScope.addSVar("DesugarInOut_0_0", new InteropSDefT(classLoader, "DesugarJ$$Desugar$In$Out_0_0", context));
      varScope.addSVar("main_0_0", new InteropSDefT(classLoader, "DesugarJ$main_0_0", context));
    }
  }
}